package com.apesource.bookBuy.view;

import com.apesource.bookBuy.bean.User;
import com.apesource.bookBuy.dao.DaoUtil;
import com.apesource.bookBuy.mapper.UserMapper;
import com.apesource.bookBuy.utils.SendEmailUtil;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import org.apache.ibatis.session.SqlSession;

import java.util.UUID;

/**
 * @version 1.0
 * @auther:暮雪
 * @date:2025/9/5
 */
@Path("/users")
public class UsersController extends Controller {
    public void index() {
        if(getSessionAttr("admin") != null) {
            redirect("/book");
        }else redirect("/users/login");
    }

    //登录
    public void login() {
        render("/login.html");
    }

    public void execu_login() {
        SqlSession sqlSession= DaoUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        String userEmail = getPara("userEmail");
        String password = getPara("pwd");

        User user = userMapper.findUser(userEmail, password);
        System.out.println("login==============:"+user);
        if(user != null) {
            setSessionAttr("users", user);
            redirect("/book");
            return;

        }redirect("/users/login");
        DaoUtil.close(sqlSession);


    }

    //注册
    public void register() {
        render("/register.html");
    }

    public void execu_register() {
        SqlSession sqlSession= DaoUtil.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        String username = getPara("username");
        String password = getPara("password");
        String userEmail=getPara("userEmail");

        User user=new User();
        user.setUseremail(userEmail);
        user.setUsername(username);
        user.setUserpassword(password);

        int res=userMapper.registerUser(user);
        if(res > 0) {
            sqlSession.commit();

            redirect("/users/login");
        }else renderText("注册失败");
        DaoUtil.close(sqlSession);
    }

    public void forghtPwd(){
        render("/forget.html");
    }

    public void exec_forghtPwd(){
        String email = getPara("email");
        String code= UUID.randomUUID().toString().substring(0,6);

        setSessionAttr("email",email);
        setSessionAttr("code",code);

        boolean idOk=SendEmailUtil.sendEmail(email,code);
        if(idOk) {
            System.out.println("发送成功");
            redirect("/users/verification");

        }else redirect("/users/forghtPwd");

    }

    public void verification(){
        render("/verification.html");
    }

    public void execu_verification(){

        String receivecode=get("code");
        String newPwd=get("pwd");


        String email=getSessionAttr("email");
        String code=getSessionAttr("code");
        if(code.equals(receivecode)){
            SqlSession sqlSession=DaoUtil.getSqlSession();
            UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

            int res=userMapper.updateUserPasswd(email,newPwd);
            if(res > 0) {
                sqlSession.commit();
                redirect("/users/login");
            }else renderHtml("<h1>错误</h1>");
        }else redirect("/users/verification");
    }

    public void logout(){

        setSessionAttr("users", null);
        redirect("/users/login");
    }
}
